﻿<?php
     
class Uuch_Reader_YixunReader extends Uuch_Reader_ProductReader{
    
    public function getInfo($url) {
        $client = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response = $client->request();
        $body = $response->getBody();
        
        $uri = parse_url($url);
        $name = explode('.',$uri['host']);
        $reader = ucfirst(strtolower($name[count($name)-2]));
        $config = Application_Model_ReaderConfig::getBySite($reader);
        
        //Name    #itemprop="name">([\W\w.]+?)<#i   
        $product = new Application_Model_Product();
        $matches = array();
        if(preg_match($config->ProductNameRegex, $body,$matches)){
            $product->Name = $matches[1];
        }
        
        
        
        //price        #itemprop="price"><i>&yen;</i>(.+?)<#i        
        if(preg_match($config->PriceRegex, $body,$matches)){
            $product->Price = $matches[2];
        }
        
        if($product->Price >= 29){
            $product->ShipFee = 0;
        }else{
            $product->ShipFee = 5;
        }
        
        //img           #id="xgalleryImg" src="(.+?)" data-#im
        
        if(preg_match($config->ImageRegex, $body,$matches)){
            $product->Image = $matches[1];
            $product->RemoteImage = $product->Image;
        }
        
        if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $shopName = $matches[1];
        }
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id= $matches[1];
        }
        
        $product->Sku = $id;
        $product->Url = $url;
        
        
        $product->Quantity = 999;
                
        
        $shopName = '易迅网';        
        
        $shop = Application_Model_Shop::getByName($shopName);
        if (NULL == $shop){
            $shop = new Application_Model_Shop();
            $shop->Name = $shopName;
            $shop->IsTaobao = True;
            $shop->Url = $uri['host'];
            $shop->save();
        }
        $product->ShopId = $shop->Id;
        $product->save();
        return $product;
    }
    
	
	public function getInfoForNew($url) {
        $client = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response = $client->request();
        $body = $response->getBody();
        
        $uri = parse_url($url);
        $name = explode('.',$uri['host']);
        $reader = ucfirst(strtolower($name[count($name)-2]));
        $config = Application_Model_ReaderConfig::getBySite($reader);
        
        //Name    #itemprop="name">([\W\w.]+?)<#i   
        $product = new Application_Model_Product();
        $matches = array();
        if(preg_match($config->ProductNameRegex, $body,$matches)){
            $product->Name = $matches[1];
        }
        
        
        
        //price        #itemprop="price"><i>&yen;</i>(.+?)<#i        
        if(preg_match($config->PriceRegex, $body,$matches)){
            $product->Price = $matches[2];
        }
        
        if($product->Price >= 29){
            $product->ShipFee = 0;
        }else{
            $product->ShipFee = 5;
        }
        
        //img           #id="xgalleryImg" src="(.+?)" data-#im
        
        if(preg_match($config->ImageRegex, $body,$matches)){
            $product->Image = $matches[1];
            $product->RemoteImage = $product->Image;
        }
        
        if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $shopName = $matches[1];
        }
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id= $matches[1];
        }
        
        $product->Sku = $id;
        $product->Url = $url;
        
        
        $product->Quantity = 999;
                
        
        $shopName = '易迅网';      
		
		if (!$product->Price) {
			return NULL;
		}  
        
        $shop = Application_Model_Shop::getByName($shopName);
        if (NULL == $shop){
            $shop = new Application_Model_Shop();
            $shop->Name = $shopName;
            $shop->IsTaobao = True;
            $shop->Url = $uri['host'];
            //$shop->save();
        }
        //$product->ShopId = $shop->Id;
        //$product->save();
        //return $product;
		return array(
			'product' => $product, 
			'shopName' => $shopName,
			'siteName' => '易迅网',
			'shopUrl' => $url,
			'url' => $url,
			'siteUrl' => $uri['scheme'] . '://' . $uri['host']);
    }
}